package org.zjt.platform.demo.config;
//org.zjt.platform.demo.config

import org.zjt.platform.demo.config.properties.WebProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


/**
 * @author <a href="juntao.zhang@qq.com">juntao.zhang</a>
 * @Description:
 * @Package org.zjt.platform.demo.config
 * @date 2018/3/12 11:41
 * @see
 */
@Configuration
@EnableConfigurationProperties(WebProperties.class)
public class CorsAutoConfiguration {

    @Bean
    public CorsConfiguration corsConfiguration(WebProperties webProperties) {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin(webProperties.getCorsAllowDomain());
        corsConfiguration.addAllowedHeader(webProperties.getCorsAllowHeader());
        corsConfiguration.addAllowedMethod(webProperties.getCorsAllowMethod());
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter(CorsConfiguration corsConfiguration,WebProperties webProperties) {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration(webProperties.getCorsUrl(), corsConfiguration);
        return new CorsFilter(source);
    }

}
